package cn.gavin.web.controller;

import cn.gavin.domain.Role;
import cn.gavin.qo.QueryObject;
import cn.gavin.service.IPermissionService;
import cn.gavin.service.IRoleService;
import cn.gavin.util.RequiredPermission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;


@Controller
@RequestMapping("/role")
public class RoleController {
    @Autowired
    private IRoleService roleService;

    @Autowired
    private IPermissionService permissionService;

    @RequiredPermission(name = "角色页面",expression = "role:list")
    @RequestMapping("/list")
    public String list(Model model, QueryObject qo){
        model.addAttribute("pageInfo",roleService.query(qo));
        return "role/list";
    }

    @RequiredPermission(name = "角色删除",expression = "role:delete")
    @RequestMapping("/delete")
    public String delete(Long id){
        if (id != null) {
            roleService.deleteById(id);
        }
        return "redirect:/role/list";
    }

    @RequiredPermission(name = "角色新增/编辑",expression = "role:save")
    @RequestMapping("/input")
    public String input(Long id,Model model){
        if (id != null) {
            Role role = roleService.findById(id);
            model.addAttribute("role",role);
        }
        model.addAttribute("permissions",permissionService.listAll());
        return "role/input";
    }

    @RequiredPermission(name = "角色新增/编辑",expression = "role:save")
    @RequestMapping("/save")
    public String save(Role role,Long[] ids){
        if (role.getId() != null) {
            roleService.update(role,ids);
        }else {
            roleService.insert(role,ids);
        }
        return "redirect:/role/list";
    }
}
